import Vue from 'vue'
import VueRouter from 'vue-router'
import axios from 'axios'



Vue.prototype.$axios = axios
Vue.use(VueRouter)

const routes = [{
        path: '/',
        component: () => import('../views/Home.vue'),
        children: [{
                path: '',
                redirect: '/index'
            },
            {
                path: '/index',
                name: 'Index',
                component: () => import('../views/Index.vue')
            },
            {
                path: '/order',
                name: 'Order',
                component: () => import('../views/Order.vue')
            }, {
                path: '/me',
                name: 'Me',
                component: () => import('../views/Me.vue')
            }
        ]
    },
    {
        path: '/login',
        name: 'Login',
        component: () => import('../views/Login.vue')
    }
]

const router = new VueRouter({
    mode: 'history',
    base: process.env.BASE_URL,
    routes
})
// 路由守卫
router.beforeEach((to, from, next) => {
    const isLogin = localStorage.login ? true : false;
    if (to.path == '/login') {
        next();
    } else {
        // 查看登录状态
        isLogin ? next() : next('/login')
    }
})

export default router